package com.shxc.smarttrade.old.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.shxc.smarttrade.R;
import com.shxc.smarttrade.old.bean.EventDatas;
import com.shxc.smarttrade.old.bean.PendOrder;
import com.shxc.smarttrade.old.constants.EventContents;
import com.shxc.smarttrade.old.constants.URLContents;
import com.shxc.smarttrade.old.myview.CommonDialog;
import com.shxc.smarttrade.old.utils.MyOkHttpUtils;
import com.shxc.smarttrade.old.utils.SpUtils;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;

/**
 * Created by jcdev1 on 2017/6/5.
 */

public class PendingOrderDetailActivity extends BaseActivity {
    @BindView(R.id.rl_left)
    RelativeLayout rlLeft;
    @BindView(R.id.tv_center)
    TextView tvCenter;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_target_price)
    TextView tvTargetPrice;
    @BindView(R.id.tv_trade_direction)
    TextView tvTradeDirection;
    @BindView(R.id.tv_style)
    TextView tvStyle;
    @BindView(R.id.tv_volume)
    TextView tvVolume;
    @BindView(R.id.tv_stop_profit_price)
    TextView tvStopProfitPrice;
    @BindView(R.id.tv_stop_loss_price)
    TextView tvStopLossPrice;
    @BindView(R.id.tv_time)
    TextView tvTime;

    private PendOrder.DataBean.PendOrderBean pendOrderBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pending_order_detail);
        ButterKnife.bind(this);
        initView();
    }

    /**
     * 初始化数据
     */
    private void initView() {
        tvCenter.setText("订单详情");
        Intent intent = getIntent();
        pendOrderBean = (PendOrder.DataBean.PendOrderBean) intent.getSerializableExtra("pendOrderBean");
        tvName.setText(pendOrderBean.getSymbolCn() + " " + pendOrderBean.getSymbolEn());
        tvTargetPrice.setText(pendOrderBean.getOpenPrice() + "");
        //  cmd: 2=>’挂单买入’, 3=>’挂单卖出’, 4=>’挂单买入’, 5=>’挂单卖出’,
        if (pendOrderBean.getCmd() == 2)
            tvTradeDirection.setText("挂单买入");
        else if (pendOrderBean.getCmd() == 3)
            tvTradeDirection.setText("挂单卖出");
        else if (pendOrderBean.getCmd() == 4)
            tvTradeDirection.setText("挂单买入");
        else if (pendOrderBean.getCmd() == 5)
            tvTradeDirection.setText("挂单卖出");
        tvStyle.setText(pendOrderBean.getWay());
        tvVolume.setText(pendOrderBean.getVolume() + "");
        if (pendOrderBean.getTp() == 0)
            tvStopProfitPrice.setText("未设置");
        else tvStopProfitPrice.setText(pendOrderBean.getTp() + "");

        if (pendOrderBean.getSl() == 0)
            tvStopLossPrice.setText("未设置");
        else tvStopLossPrice.setText(pendOrderBean.getSl() + "");

        tvTime.setText(pendOrderBean.getOpenTime());
    }

    @OnClick({R.id.rl_left, R.id.btn_delete})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rl_left:
                finish();
                break;
            case R.id.btn_delete:
                CommonDialog commonDialog = new CommonDialog(this, R.style.customDialogStyle);
                commonDialog.setCanceledOnTouchOutside(true);
                commonDialog.setBaseText("提示", "确定要撤销此挂单吗？", "取消", "确定");
                commonDialog.setYesCLickRunAble(new Runnable() {
                    @Override
                    public void run() {
                        showShortDialog();
                        deletePendingOrder(pendOrderBean.getTicket() + "", SpUtils.getInt(getApplicationContext(), "userId"));
                    }
                });
                commonDialog.show();
                break;
        }
    }

    /**
     * 删除挂单
     *
     * @param ticket
     * @param userId
     */
    private void deletePendingOrder(String ticket, int userId) {
        Map<String, Object> map = new HashMap<>();
        map.put("accessToken", SpUtils.getString(this, "accessToken", ""));
        map.put("ticket", ticket);
        map.put("userId", userId + "");
        MyOkHttpUtils.getServiceDatas(this,map, URLContents.PENDING_ORDER_DETAIL_ACTIVITY_DELETE, new MyOkHttpUtils.ServiceStatus() {
            @Override
            public void faild(Call call, Exception e, int id) {
                showToast(R.string.net_error);
                dismissShortDialog();
            }

            @Override
            public void success(String response, int id) {
                if (response != null) {
                    JsonObject jsonObject = new JsonParser().parse(response).getAsJsonObject();
                    String errCode = "";
                    String errMsg = "";
                    if (!jsonObject.get("err_code").isJsonNull())
                        errCode = jsonObject.get("err_code").getAsString();
                    if (!jsonObject.get("err_msg").isJsonNull())
                        errMsg = jsonObject.get("err_msg").getAsString();
                    if (errCode.equals("0")) {
                        showToast("撤销成功");
                        finish();
                        //通知上一个界面刷新数据
                        EventBus.getDefault().post(new EventDatas(EventContents.PENDING_OEDER_DETAIL, null));
                    } else
                        showToast(errMsg);

                }
                dismissShortDialog();
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

    }
}
